package com.dyz.wsw.common.utils.converter;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 日期字符转Instant
 * @author dyz
 * Time 2024/11/24 10:35
 */
public class InstantConverter {

    /**
     * 日期转Instant
     * @param datetime 符合特定规范的时间字符，如2011-12-03T10:15:30', '2011-12-03T10:15:30+01:00' or '2011-12-03T10:15:30+01:00
     * @return 转换后的Instant对象
     */
    public static Instant toInstant(String datetime){
        LocalDateTime localDateTime = LocalDateTime.parse(datetime, DateTimeFormatter.ISO_DATE_TIME);
        //TODO 后期看情况思考如何处理时间更为合理
        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("Asia/Shanghai"));
        return zonedDateTime.toInstant();
    }
}
